package com.sc.nft.dao;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.DepositWithdrawalRecord;
import com.sc.nft.entity.dto.SaveWithdrawRecordCauseDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.ApprovalTypeEnum;
import com.sc.nft.enums.DepositWithdrawalRecordPayStatusEnum;
import com.sc.nft.enums.DepositWithdrawalRecordStatusEnum;
import com.sc.nft.enums.DepositWithdrawalRecordTypeEnum;
import com.sc.nft.mapper.DepositWithdrawalRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * (sc_deposit_withdrawal_record)数据DAO
 *
 * @author Sun
 * @since 2022-09-07 12:59:49
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class DepositWithdrawalRecordDao extends ServiceImpl<DepositWithdrawalRecordMapper, DepositWithdrawalRecord> {


    public Boolean updateStatus(Long id, DepositWithdrawalRecordStatusEnum statusEnum, String remark) {
        LambdaUpdateWrapper<DepositWithdrawalRecord> wrapper=new LambdaUpdateWrapper();
        wrapper.eq(DepositWithdrawalRecord::getId,id);
        wrapper.eq(DepositWithdrawalRecord::getStatus,DepositWithdrawalRecordStatusEnum.APPROVAL);
        wrapper.eq(DepositWithdrawalRecord::getPayStatus,DepositWithdrawalRecordPayStatusEnum.APPROVAL);
        wrapper.setSql("status="+statusEnum.getCode()+",remark ="+remark);
        return update(wrapper);
    }






    public Page<NoAuditRechargeRecordVO> getNoAuditRechargeRecord(int pageNo, int pageSize) {

        return baseMapper.getNoAuditRechargeRecord(new Page(pageNo,pageSize));
    }

    public Page<WithdrawOrRechargeRecordVO> withdrawalsOrRechargeRecord(Long userId, String calendar, Integer pageNo, Integer pageSize, DepositWithdrawalRecordTypeEnum withdraw) {
        return baseMapper.withdrawalsOrRechargeRecord(userId,calendar,withdraw,new Page(pageNo,pageSize));
    }

    public Page<RechargeRecordVO> getRechargeRecord(Date startTime, Date endTime, String orderNo, String payStatus, String userTel, int pageNo, int pageSize) {
        DepositWithdrawalRecordPayStatusEnum payStatusEnum = null;
        if(StrUtil.isNotBlank(payStatus)){
            payStatusEnum = DepositWithdrawalRecordPayStatusEnum.valueOf(payStatus);
        }
        return baseMapper.getRechargeRecord(startTime,endTime,orderNo,payStatusEnum,userTel,new Page(pageNo,pageSize));
    }



    public Page<NoRefundRechargeRecordVO> getNoRefundRechargeRecord(int pageNo, int pageSize) {
        return baseMapper.getNoRefundRechargeRecord(new Page(pageNo,pageSize));
    }

    public NoRefundRechargeRecordByIdVO getNoRefundRechargeRecordById(Long id) {
        return baseMapper.getNoRefundRechargeRecordById(id);
    }

    public NoAuditRechargeRecordByIdVO getNoAuditRechargeRecordById(Long id) {
        return baseMapper.getNoAuditRechargeRecordById(id);
    }

    public Boolean getByUserIdExists(Long userId) {
        return baseMapper.getByUserIdExists(userId);
    }

    public void saveWithdrawRecordCause(SaveWithdrawRecordCauseDTO request) {
        UpdateWrapper<DepositWithdrawalRecord> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", request.getId());
        updateWrapper.set("cause", request.getCause());
        update(updateWrapper);
    }

    public BigDecimal getSumRecharge() {
        return baseMapper.getSumRecharge();
    }

    public BigDecimal getSumWithdraw() {
        return baseMapper.getSumWithdraw();
    }

    public BigDecimal getToDayRechargeAmount() {
        return baseMapper.getToDayRechargeAmount();
    }

    public BigDecimal getSumRechargeAmount() {
        return baseMapper.getSumRechargeAmount();
    }

    public BigDecimal getToDayRechargeAmountByDay(String day) {
        return baseMapper.getToDayRechargeAmountByDay(day);
    }

    public static void main(String[] args) {
        int min =50001;
         String sql = " insert into user_amount (select id, (select ifnull(sum(pay_amount),0) from sc_pay_order where status=2 and s.id=user_id) as '总入金',\n" +
                "(select balance from sc_user_wallet where s.id=user_id) as '总余额',\n" +
                "(select pay_balance from sc_user_wallet where s.id=user_id) as '总红包余额',\n" +
                "(select ifnull(sum(amount),0) from sc_sand_withdraw_record where user_id=s.id and status=1) as '总转账',\n" +
                "(select ifnull(sum(amount),0) from sc_user_wallet_detail where user_id=s.id and type=2) as '历史提现'\n" +
                " from sc_user_info s where is_delete=0 and id not in (select user_id from sc_filter_income_list where is_delete=0) and s.id between %s and %s);";
        for (int i = 0; i < 85; i++) {
            System.out.println(String.format(sql,min,min+99));
            min = min+100;
        }
    }

}